#!/bin/sh
. /etc/nap.conf
ip=`echo $1 | sed -e 's/[^0-9\.]*//g'`
if [ "$ip" = "" ]
then
	echo error no ip address
	return
fi

if [ "$2" -gt 1024 ]
then
	sendport=$2
fi

if [ "$3" -gt 1024 ]
then
	recvport=$3
fi

if [ "$sendport" = "" ] || [ "$recvport" = "" ] || [ "$sendport" -lt 1024 ] || [ "$recvport" -lt 1024 ]
then
	echo error missing send or receive port
	return
fi

naprtpstop 2>/dev/null
arecord -f cd -c 1 -r 4000 -t raw | rtpsend-arm - $ip $recvport >> "$napdata/naprtpsend.log" 2>&1 &
# this pause is critical or aplay will fail - why?
sleep 1
rtprecv-arm - $sendport | aplay -f cd -t raw -c 1 -r 4000 >> "$napdata/naprtprecv.log" 2>&1 &

response=`wget -q -O - "http://$ip/connect.cgi?sendport=$sendport\&recvport=$recvport"`
if [ "$response" = "CONNECTED" ]
then
	echo connected to $ip sendport $sendport recvport $recvport
else
	naprtpstop
	echo error connecting $response
fi
